package cn.nubia.admin.sysconfig.service;

import cn.nubia.admin.common.mybatis.bean.Query;
import cn.nubia.admin.common.mybatis.service.BaseServiceImpl;
import cn.nubia.admin.sysconfig.bean.Role;
import cn.nubia.admin.sysconfig.bean.RoleUser;
import cn.nubia.admin.sysconfig.bean.SystemLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by Jason on 2015/7/4.
 */
@Service
public class RoleUserService extends BaseServiceImpl<RoleUser> {

    @Autowired
    private RoleService roleService;
    @Autowired
    SystemLogService systemLogService;


    /**
     * 通过英
     *
     * @param userId
     * @return
     */
    public Role getRoleByUserId(Integer userId) {
        Role role = new Role();

        Query<RoleUser> query = Query.build(RoleUser.class);
        query.addEq("userId", userId);
        RoleUser roleUser = get(query);
        if (roleUser != null) {
            Integer roleId = roleUser.getRoleId();
            role = roleService.get(roleId);
        }
        return role;
    }

    /**
     *保存用户角色关系
     * @param roleUser 用户角色关系.
     */
    public void save(RoleUser roleUser) {
        Query<RoleUser> roleUserQuery = Query.build(RoleUser.class);
        roleUserQuery.addEq("userId",roleUser.getUserId());

        RoleUser roleUser1 = get(roleUserQuery);
        if (null == roleUser1) {
            insert(roleUser);
            systemLogService.log(SystemLog.ModuleTypeEnum.ROLEUSERLIST, SystemLog.OperateTypeEnum.ADD, roleUser.getId());

        } else {
            roleUser1.setRoleId(roleUser.getRoleId());
            update(roleUser1);
            systemLogService.log(SystemLog.ModuleTypeEnum.ROLEUSERLIST, SystemLog.OperateTypeEnum.EDIT, roleUser1.getId());

        }

    }
}
